package com.neuedu.javase.io;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/***
 * 项目名称:  MyJavaSE 
 * 包：  com.neuedu.javase.io 
 * date:   2025/11/6   8:47  
 * 描述:  
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class MyFileReader {

    public static void main(String[] args) {
        // 使用字符流
        String file = "d:\\upload\\introduce.txt";
        FileReader fr = null;
        try {
            fr = new FileReader(file);


            //一次读一个字符
            // char readChar = (char) fr.read();

            // System.out.println((char) fr.read());
            // System.out.println((char) fr.read());
            // System.out.println((char) fr.read());
            // System.out.println((char) fr.read());

            //使用缓冲区读取
            char[] buf = new char[1024];
            StringBuffer sbuffer = new StringBuffer();
            int len = 0;
            while (   (len = fr.read(buf)) != -1 ){
                sbuffer.append(buf, 0, len);
            }

            System.out.println("读取到的内容:\r\n" + sbuffer.toString());


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }


    }
}
